const QQMapWX = require('../Tools/qqmap-wx-jssdk.min.js');
const qqmapsdk = new QQMapWX({key: "5QUBZ-XZVW6-5U7SE-M4OZW-VA7DE-WXFZ6"});
const tool=require('../Tools/HTTPOpertion.js');
const $={
  login:function(finish){
    tool.login().then(function (res) {
      res.token && wx.setStorageSync('token', res.token);
      finish();
    }).catch(function (err) {
      wx.showToast({title:err.errorMsg||'网络错误，请稍后再试',icon:'none'})
    });
  },
  loginBack:function(){
    const path=wx.getStorageSync('path')||'/pages/spellGroupHome/spellGroupHome';
    console.log(path)
    if(path){
      wx.removeStorageSync('path')
    }
    const router=path.indexOf('?')>0?path.split('?')[0]:path;
    if(router.indexOf('spellGroupHome')>0||router.indexOf('about')>0||router.indexOf('my')>0){
      wx.reLaunch({
        url: router
      })
    }else{
      wx.reLaunch({
        url:path
      })
    }
  },
  getUserLocation: function (type,callback) {
    var city='';
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        wx.setStorageSync("latitude", res.latitude);
        wx.setStorageSync("longitude", res.longitude);
        // 经纬度位置转换
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            var address = addressRes.result.formatted_addresses.district;
            city = addressRes.result.address_component.city;
            if(type=='homePage'){
              callback(addressRes.result.address_component.district);
              return false;
            }else{
              wx.setStorageSync("city", city );
              $.getCityId(city,callback)
            }
          },
          fail: function (err) {
            console.log(err);
            city = '北京';
            wx.setStorageSync("city", city);
            wx.setStorageSync('codeid', '110100')
            var currentpage = getCurrentPages();
            $.setpageData(currentpage, city);
          }
        })
      },
      // 获取位置失败，定位到北京市天an门
      fail:function(err){
        city = '北京' ;
        wx.setStorageSync("city", city);
        wx.setStorageSync('codeid', '110100')
        var currentpage = getCurrentPages();
        $.setpageData(currentpage, city);
      }
    })
  },
  getCityId:function(ci,callback){
    var codeid = '',city;
    tool.getCityList({}).then(function (res) {
      var citylist = res.data.xcxCCPPCityList;
      var i = 0;
      while (i < citylist.length) {
        var cis = citylist[i].cityname + "市";
        if (ci == cis) {
          codeid = citylist[i].id;
          city = citylist[i].cityname;
          console.log("获取城市成功"+city)
          if(!wx.getStorageSync('codeid')){
              var currentpage = getCurrentPages();
              console.log(currentpage);
              $.setpageData(currentpage,city);
          }
          wx.setStorageSync('codeid', codeid)
          wx.setStorageSync('citybox', codeid)
          callback()
          break;
        }
        i++;
      }
    })
    .catch(err=>{
      console.log(err)
    })
  },
  setpageData: function (data,city) {
    if (data.length > 0) {
      if (data[0].route.indexOf('spellGroupHome/spellGroupHome') > 0) {
        data[0].setData({ city: city })
      }
    } else {
      setTimeout(function () {
        data = getCurrentPages()
        $.setpageData(data,city)
      }, 50)
    }
  }

}
module.exports=$;
